/**
 * 
 */
package cn.edu.cqu.bysj.wm.gui.result;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.neo4j.graphdb.Node;

import cn.edu.cqu.bysj.wm.ago.anlys.Cluster;
import cn.edu.cqu.bysj.wm.ago.anlys.ModularityGN;
import cn.edu.cqu.bysj.wm.ago.anlys.ModularityLP;
import cn.edu.cqu.bysj.wm.ago.anlys.Similarity;
import cn.edu.cqu.bysj.wm.ago.anlys.Strength;
import cn.edu.cqu.bysj.wm.ago.gn.GN;
import cn.edu.cqu.bysj.wm.ago.gn.GNCommunity;
import cn.edu.cqu.bysj.wm.ago.lp.LP;
import cn.edu.cqu.bysj.wm.ago.lp.LPCommunity;
import cn.edu.cqu.bysj.wm.keynode.CentralityTool;
import cn.edu.cqu.bysj.wm.keynode.KeyNodes;
import cn.edu.cqu.bysj.wm.util.DoubleFormat;

/**
 * @author Meng
 * 
 */
public class DetectedResult {

	private static LPAlgorithmResult lpAlgorithmResult;
	private static GNAlgorithmResult gnAlgorithmResult;
	private static SolutionsResult SolutionsResult;
	private static GlobalKeyNodeResult globalKeyNodeResult;
	private static LocalKeyNodeResult localKeyNodeResult;

	public static GlobalKeyNodeResult getGlobalKeyNodeResult() {
		return globalKeyNodeResult;
	}

	public static void setGlobalKeyNodeResult(List<Node> keyNodes,
			long totalTime, int[] flag, CentralityTool cTool) {
		globalKeyNodeResult = new GlobalKeyNodeResult();
		globalKeyNodeResult.setTotalTime(totalTime);
		globalKeyNodeResult.setKeyNodeNumber(keyNodes.size());
		List<NodeRankRow> nodeRankRows = new ArrayList<NodeRankRow>();
		for (int i = 0; i < keyNodes.size(); i++) {
			NodeRankRow row = new NodeRankRow();
			for (int j = 0; j < flag.length; j++) {
				if (flag[j] == 1) {
					switch (j) {
					case 0:
						row.setDegree(cTool.rankNode[i][j]);
						break;
					case 1:
						row.setBetweenness(cTool.rankNode[i][j]);
						break;
					case 2:
						row.setStress(cTool.rankNode[i][j]);
						break;
					case 3:
						row.setCloseness(cTool.rankNode[i][j]);
						break;
					case 4:
						row.setCluster(cTool.rankNode[i][j]);
						break;
					case 5:
						row.setPagerank(cTool.rankNode[i][j]);
						break;
					}
				}

			}
			nodeRankRows.add(row);
		}
		globalKeyNodeResult.setNodeRankRows(nodeRankRows);
		globalKeyNodeResult.setKeyNodes(keyNodes);
		globalKeyNodeResult.setSpearman(KeyNodes.spearmanCoefficient);
	}

	public static LocalKeyNodeResult getLocalKeyNodeResult() {
		return localKeyNodeResult;
	}

	public static void setLocalKeyNodeResult(Map<Long, List<Node>> keyNodes,
			long totalTime) {
		localKeyNodeResult = new LocalKeyNodeResult();
		localKeyNodeResult.setTotalTime(totalTime);
		List<CommunityKeyNodeRow> communityKeyNodeRows = new ArrayList<CommunityKeyNodeRow>();
		for (long cid : keyNodes.keySet()) {
			CommunityKeyNodeRow row = new CommunityKeyNodeRow();
			List<Node> list = keyNodes.get(cid);
			row.setCid(cid);
			row.setKeyNodesNumber(list.size());
			row.setSize(LPCommunity.communitys.get(cid).size());
			row.setKeyNodes(list);
			communityKeyNodeRows.add(row);
		}
		localKeyNodeResult.setCommunityKeyNodeRows(communityKeyNodeRows);
	}

	public static LPAlgorithmResult getLPAlgorithmResult() {
		return lpAlgorithmResult;
	}

	public static void setLPAlgorithmResult(LP lp) {
		lpAlgorithmResult = new LPAlgorithmResult();
		lpAlgorithmResult.setLp(lp);
		lpAlgorithmResult.setInitTime(lp.initTime);
		lpAlgorithmResult.setIterTime(lp.iterTime);
		lpAlgorithmResult.setTotalTime(lp.initTime + lp.iterTime);
		lpAlgorithmResult.setNodesNumber(lp.n);
		lpAlgorithmResult.setEdgesNumber(lp.e);

		List<CommunityRow> communityRows = new ArrayList<CommunityRow>();
		for (long cid : LPCommunity.communitys.keySet()) {
			CommunityRow row = new CommunityRow();

			List<Node> list = LPCommunity.communitys.get(cid);

			row.setCid(cid);
			row.setSize(list.size());
			row.setCluster(DoubleFormat
					.doubleFormat(Cluster.clusterCoefficients.get(cid)));
			row.setStrength(Strength.strengths.get(cid));
			row.setMembers(list);
			communityRows.add(row);
		}
		lpAlgorithmResult.setCommunityRows(communityRows);
		lpAlgorithmResult.setQ(DoubleFormat.doubleFormat(ModularityLP.Q));
	}

	public static SolutionsResult getSolutionsResult() {
		return SolutionsResult;
	}

	public static void setSolutionResult() {
		SolutionsResult = new SolutionsResult();

		List<SolutionRow> solutionRows = new ArrayList<SolutionRow>();
		for (int sid : Similarity.solutions.keySet()) {
			SolutionRow row = new SolutionRow();
			row.setSid(sid);
			row.setQ(DoubleFormat.doubleFormat(Similarity.Qs.get(sid)));
			row.setCommunityNumber(Similarity.solutions.get(sid).size());
			solutionRows.add(row);
		}
		SolutionsResult.setSolutionRows(solutionRows);
		SolutionsResult.setMatrix(Similarity.M);
	}

	public static GNAlgorithmResult getGNAlgorithmResult() {
		return gnAlgorithmResult;
	}

	public static void setGNAlgorithmResult(GN gn) {
		gnAlgorithmResult = new GNAlgorithmResult();

		gnAlgorithmResult.setInitTime(gn.initTime);
		gnAlgorithmResult.setIterTime(gn.iterTime);
		gnAlgorithmResult.setbTime(gn.bTime);
		gnAlgorithmResult.setTotalTime(gn.initTime + gn.iterTime + gn.bTime);
		gnAlgorithmResult.setDepth(GNCommunity.hTree.getDepth());
		gnAlgorithmResult.setMaxQ(ModularityGN.maxQ);
		gnAlgorithmResult.setMaxDepth(ModularityGN.depth);
		gnAlgorithmResult.setNodesNumber(gn.n);
		gnAlgorithmResult.setEdgesNumber(gn.e);

		List<TreeRow> treeRows = new ArrayList<TreeRow>();
		for (long cid : GNCommunity.communitys.keySet()) {
			TreeRow row = new TreeRow();
			List<Node> list = GNCommunity.communitys.get(cid);
			row.setCid(cid);
			row.setSize(list.size());
			row.setPid(GNCommunity.hTree.getParent(cid));
			row.setLevel(GNCommunity.hTree.getLevel(cid));
			treeRows.add(row);
		}
		gnAlgorithmResult.setTreeRows(treeRows);

		List<DepthQRow> depthQRows = new ArrayList<DepthQRow>();
		for (int d : ModularityGN.Qs.keySet()) {
			DepthQRow row = new DepthQRow();
			row.setDepth(d);
			row.setQ(DoubleFormat.doubleFormat(ModularityGN.Qs.get(d)));
			row.setCommunityNumber(ModularityGN.depthCommunity.get(d).size());
			depthQRows.add(row);
		}
		gnAlgorithmResult.setDepthQRows(depthQRows);

		List<CommunityRow> communityRows = new ArrayList<CommunityRow>();
		for (Node node : ModularityGN.depthCommunity.get(ModularityGN.depth)) {
			CommunityRow row = new CommunityRow();
			long cid = node.getId();
			List<Node> list = GNCommunity.communitys.get(cid);
			row.setCid(cid);
			row.setSize(GNCommunity.communitys.get(cid).size());
			row.setCluster(DoubleFormat
					.doubleFormat(Cluster.clusterCoefficients.get(cid)));
			row.setStrength(Strength.strengths.get(node.getId()));
			row.setMembers(list);
			communityRows.add(row);
		}
		gnAlgorithmResult.setCommunityRows(communityRows);
	}

}
